package com.magist.manage.copyrights.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import tk.mybatis.mapper.entity.Example;


import com.magist.manage.copyrights.entity.AppendixProject;
import com.magist.manage.copyrights.dto.AppendixProjectDto;
import com.magist.manage.copyrights.service.AppendixProjectService;

@ApiIgnore
@RestController
@RequestMapping("/rest/appendixProject")
public class RestAppendixProjectController extends BaseController {

	@Autowired
	private AppendixProjectService appendixProjectService;

	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("appendixProjectDto") AppendixProjectDto appendixProjectDto) {
		AppendixProject appendixProject = new AppendixProject();
		BeanUtils.copyProperties(appendixProjectDto,appendixProject);
		appendixProject.setId(appendixProjectService.getNewId());
		appendixProjectService.insert(appendixProject);
		return ResultMessage.success();
	}

	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("appendixProjectDto") AppendixProjectDto appendixProjectDto) {
		AppendixProject oldAppendixProject = appendixProjectService.getById(appendixProjectDto.getId());
		if(oldAppendixProject==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		AppendixProject appendixProject = new AppendixProject();
		BeanUtils.copyProperties(appendixProjectDto,appendixProject);
		appendixProject.setModifyTime(oldAppendixProject.getModifyTime());
		if (appendixProjectService.update(appendixProject)>0) 
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("appendixProjectDto") AppendixProjectDto appendixProjectDto) {
		ResultMessage result = ResultMessage.success();
		Example example = new Example(AppendixProject.class);
		example = packExample(example,appendixProjectDto);
		PageInfo<AppendixProject> pageInfo = appendixProjectService.page(example,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		AppendixProject appendixProject = appendixProjectService.getById(id);
		if(appendixProject==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(appendixProjectService.delete(appendixProject)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		AppendixProject appendixProject = appendixProjectService.getById(id);
		if(appendixProject==null)
			throw new BusinessException("无效的数据!");
		AppendixProjectDto appendixProjectDto = new AppendixProjectDto();
		BeanUtils.copyProperties(appendixProject,appendixProjectDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(appendixProjectDto);
		return result;
	}

	private Example packExample(Example example,AppendixProjectDto appendixProjectDto){
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted","F");
		if(StringUtils.isNotEmpty(appendixProjectDto.getId()))
			criteria.andEqualTo("id",appendixProjectDto.getId());
		return example;
	}

}
